package com.example.xgakj.newterminal.base;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.example.xgakj.mylibrary.mvp.utils.SharedPreferencesUtil;
import com.example.xgakj.newterminal.BuildConfig;
import com.example.xgakj.newterminal.beans.BaoAnBean;
import com.example.xgakj.newterminal.beans.DeviceBean;
import com.example.xgakj.newterminal.pushmodule.OnlineService;
import com.jovision.tools.AppConsts;
import com.jovision.tools.IHandlerLikeNotify;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.interceptor.HttpLoggingInterceptor;
import com.socks.library.KLog;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
import com.tencent.bugly.Bugly;

import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import okhttp3.OkHttpClient;

/**
 * @author Administrator
 * @date 2017-10-15
 */

public class BaseApplication extends Application implements IHandlerLikeNotify {
    private static int fangqu;
    public static String name;
    public static int flag;
    public static String classKey;
    public static String classValue;
    private RefWatcher refWatcher;
    public static Context context;
    public static String baseUrl;
    public static String ssn;
    public static String usrID;
    public static BaoAnBean mBaoAnBean;
    private IHandlerLikeNotify currentNotifyer;
    public static DeviceBean mDeviceBean;
    @Override
    public void onCreate() {
        super.onCreate();
        context=this;
        Bugly.init(getApplicationContext(),"68780d21b2", true);
        KLog.init(BuildConfig.DEBUG);
        initNetUtil();
        if (LeakCanary.isInAnalyzerProcess(this)) {
            return;
        }
        refWatcher= LeakCanary.install(this);
        baseUrl="http://" + getIP() + ":" + getRecordPort() + "/def.aspx?";

    }

    private void initService() {
        if (SharedPreferencesUtil.getBoolean(context,Parameters.ALARM_NOTICE,true)){
            Intent intent=new Intent(context, OnlineService.class);
            intent.putExtra("CMD", "RESET");
            startService(intent);
        }
    }


    private void initNetUtil() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkGo");
//log打印级别，决定了log显示的详细程度
        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
//log颜色级别，决定了log在控制台显示的颜色
        loggingInterceptor.setColorLevel(Level.INFO);
        builder.addInterceptor(loggingInterceptor);
        //全局的读取超时时间
        builder.readTimeout(3000, TimeUnit.MILLISECONDS);
//全局的写入超时时间
        builder.writeTimeout(3000, TimeUnit.MILLISECONDS);
//全局的连接超时时间
        builder.connectTimeout(3000, TimeUnit.MILLISECONDS);
        OkGo.getInstance().init(this)
                .setOkHttpClient(builder.build())
                .setCacheMode(CacheMode.NO_CACHE)
                .setRetryCount(1);
    }

    public static String getIP(){
        return SharedPreferencesUtil.getString(context,Parameters.SERVER_IP,"");
    }
    public static String getRecordPort(){
        return SharedPreferencesUtil.getString(context,Parameters.RECORD_PORT,"");
    }

    @Override
    public void onNotify(int what, int arg1, int arg2, Object obj) {

    }
    public synchronized void onJniNotify(int what, int uchType, int channel,
                                         Object obj) {
        if (null != currentNotifyer) {
            currentNotifyer.onNotify(what, uchType, channel, obj);
        } else {
            Log.e(AppConsts.TAG_APP, "null notifyer");
        }
    }
    public void setCurrentNotifyer(IHandlerLikeNotify currentNotifyer) {
        this.currentNotifyer = currentNotifyer;
    }
    public static int getFangqu(){
        return fangqu;
    }
    public static void setFangqu(int num){
        fangqu=num;
    }

}
